package org.example.face.controller;

import com.baidu.aip.face.AipFace;
import jakarta.servlet.http.HttpServletRequest;
import org.example.face.entity.User;
import org.example.face.mapper.UserMapper;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.io.*;
import java.util.Base64;
import java.util.HashMap;

@Controller
public class FaceController {
    private static final String APP_ID = "";
    private static final String APP_KEY = "";
    private static final String SECRET_KEY = "";

    @Autowired
    private UserMapper userMapper;

    @RequestMapping(value = "register",method = RequestMethod.POST)
    @ResponseBody
    public String register(String username,String faceBase) throws IOException {
        if(!StringUtils.isEmpty(username)&&!StringUtils.isEmpty(faceBase)){
            String upPath = "D:\\hoppes3\\IDEAprojects\\face\\src\\main\\resources\\static\\photo";
            String fileName = username + "_" +System.currentTimeMillis()+".jpg";
            String path = upPath + "/" + fileName;
            File file = new File(path);
            AipFace client = new AipFace(APP_ID, APP_KEY, SECRET_KEY);
            User user = new User();
            user.setUsername(username);
            user.setPhoto(path);
            User exitUser = userMapper.findUserByName(username);
            if(exitUser!=null){
                return "2";
            }
            userMapper.addUser(user);
            byte[] Bytes = Base64.getDecoder().decode(faceBase);
            OutputStream out = new FileOutputStream(path);
            out.write(Bytes);
            HashMap<String,String> options = new HashMap<>();
            options.put("quality_control","LOW");
            options.put("liveness_control","LOW");
            String imageType = "BASE64";
            String groupId = "1001";
            JSONObject jsonObject = client.addUser(faceBase, imageType, groupId, username, options);
            System.out.println(jsonObject.toString(2));
        }
        return "1";
    }

    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ResponseBody
    public String login(String faceBase, HttpServletRequest request){
        String faceData = faceBase;
        AipFace client = new AipFace(APP_ID, APP_KEY, SECRET_KEY);
        JSONObject user = verifyUser(faceData,client);
        Double score = (Double) user.get("score");
        if(score>95){
            return "{\"num\":\"1\",\"username\":\""+user.getString("user_id")+"\"}";
        }else{
            return "{\"num\":\"2\"}";
        }
    }

    public JSONObject verifyUser(String imgBase64,AipFace client){
        HashMap<String,String> options = new HashMap<>();
        options.put("quality_control","LOW");
        options.put("liveness_control","LOW");
        JSONObject res = client.search(imgBase64,"BASE64","1001",options);
        System.out.println(res.toString(2));
        System.out.println(res.getJSONObject("result"));
        System.out.println(res.getJSONObject("result").getJSONArray("user_list"));
        JSONObject user = (JSONObject) res.getJSONObject("result").getJSONArray("user_list").get(0);
        System.out.println("百度返回的user对象："+user.toString());
        System.out.println("username:"+user.getString("user_id"));
        return user;
    }

}
